package com.gxa.exception;

import com.gxa.common.ResponseData;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * User: zdk
 * Date: 2020/10/20 11:13
 * Description:
 * Version: V1.0
 */
@ControllerAdvice  //全局
public class ExceptionController {

    @ExceptionHandler(NumberFormatException.class)
    @ResponseBody
    public ResponseData resultNumberFormatException(){ //统一返回异常信息
        ResponseData responseData = new ResponseData();
        responseData.setCode(500);
        responseData.setMsg("数字转换异常");
        responseData.setData(null);
        return responseData;
    }

    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public ResponseData resultArithmeticException(){ //统一返回异常信息
        ResponseData responseData = new ResponseData();
        responseData.setCode(500);
        responseData.setMsg("计算除零异常");
        responseData.setData(null);
        return responseData;
    }

    @ExceptionHandler(SQLException.class)
    @ResponseBody
    public ResponseData resultSQLException(){ //统一返回异常信息
        ResponseData responseData = new ResponseData();
        responseData.setCode(500);
        responseData.setMsg("SQL异常");
        responseData.setData(null);
        return responseData;
    }

    @ExceptionHandler(IOException.class)
    @ResponseBody
    public ResponseData resultIOException(){ //统一返回异常信息
        ResponseData responseData = new ResponseData();
        responseData.setCode(500);
        responseData.setMsg("IO异常");
        responseData.setData(null);
        return responseData;
    }

    @ExceptionHandler(Throwable.class)
    @ResponseBody
    public ResponseData resultOtherException(){ //统一返回异常信息
        ResponseData responseData = new ResponseData();
        responseData.setCode(400);
        responseData.setMsg("未知错误");
        responseData.setData(null);
        return responseData;
    }
    @ExceptionHandler(UnknownAccountException.class)
    @ResponseBody
    public ResponseData resultUnknownAccountException(){ //统一返回异常信息
        ResponseData responseData = new ResponseData();
        responseData.setCode(400);
        responseData.setMsg("用户名或密码错误");
        responseData.setData(null);
        return responseData;
    }
    @ExceptionHandler(IncorrectCredentialsException.class)
    @ResponseBody
    public ResponseData resultIncorrectCredentialsException(){ //统一返回异常信息
        ResponseData responseData = new ResponseData();
        responseData.setCode(400);
        responseData.setMsg("用户名或密码错误");
        responseData.setData(null);
        return responseData;
    }
    @ExceptionHandler(UnauthorizedException.class)
    @ResponseBody
    public ResponseData resultUnauthorizedException(){ //统一返回异常信息
        ResponseData responseData = new ResponseData();
        responseData.setCode(400);
        responseData.setMsg("角色错误");
        responseData.setData(null);
        return responseData;
    }
    @ExceptionHandler(LockedAccountException.class)
    @ResponseBody
    public ResponseData resultLockedAccountException(){ //统一返回异常信息
        ResponseData responseData = new ResponseData();
        responseData.setCode(400);
        responseData.setMsg("账号被锁");
        responseData.setData(null);
        return responseData;
    }
    @ExceptionHandler(AuthenticationException.class)
    @ResponseBody
    public ResponseData resultAuthenticationException(){ //统一返回异常信息
        ResponseData responseData = new ResponseData();
        responseData.setCode(400);
        responseData.setMsg("认证错误");
        responseData.setData(null);
        return responseData;
    }
    @ExceptionHandler(FileUploadException.class)
    @ResponseBody
    public ResponseData resultFileUploadException(){ //统一返回异常信息
        ResponseData responseData = new ResponseData();
        responseData.setCode(400);
        responseData.setMsg("文件上传失败");
        responseData.setData(null);
        return responseData;
    }
    @ExceptionHandler(ClassCastException.class)
    @ResponseBody
    public ResponseData resultClassCastException(){ //统一返回异常信息
        ResponseData responseData = new ResponseData();
        responseData.setCode(400);
        responseData.setMsg("操作失败请检查数据库中是否有数据");
        responseData.setData(null);
        return responseData;
    }
}
